package com.stox.ui.javafx.charting.model.plot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.stox.dto.AbstractModel;
import com.stox.ui.javafx.charting.model.uiunit.NumberUiUnit;
import com.stox.ui.javafx.charting.strategy.DefaultNumberUiUnitColorStrategy;
import com.stox.ui.javafx.charting.widget.Chart;

@Component
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class NumberPlot extends AbstractPlot<Number, 
																Chart<NumberPlot>, 
																NumberUiUnit, 
																NumberPlot> {

	public NumberPlot(AbstractModel model) {
		super(model);
	}
	
	
	@Autowired
	public void setApplicationContext(ApplicationContext applicationContext){
		setColorStrategy(applicationContext.getBean(DefaultNumberUiUnitColorStrategy.class));
	}

	@Override
	public void update() {
		super.update();
		
		for(int index = getChart().getChartingView().getEndIndex()-1; 
				index >= getChart().getChartingView().getStartIndex(); index--) {
			Number data = getData().get(index);
			if(null == data) continue;
			double value = data.doubleValue();
			if(highest < value)	highest = value;
			if(lowest > value) lowest = value;
		}
		
		for(int index = getChart().getChartingView().getEndIndex()-1; 
				index >= getChart().getChartingView().getStartIndex(); index--) {
			getUiUnits().get(index).update();
		}
	}
	
	@Override
	public void paint() {
		for(int index = getChart().getChartingView().getEndIndex()-1; 
				index >= getChart().getChartingView().getStartIndex(); index--) {
			getUiUnits().get(index).paint();
		}
	}

	@Override
	protected void createUiUnits() {
		for(int i = 0; i < getData().size(); i++)			
			getUiUnits().add(new NumberUiUnit(getData().get(i),this,i));
	}

}
